package com.gurun.codes.business.login;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.text.TextUtils;

import com.gurun.base.DebounceObservableTransformer;
import com.gurun.base.Logger;
import com.gurun.base.utils.CommonUtils;
import com.gurun.base.utils.ToastManager;
import com.gurun.codes.App;
import com.gurun.codes.R;
import com.gurun.codes.base.BaseAppCompatActivity;
import com.gurun.codes.business.home.MainActivityTwo;
import com.gurun.codes.business.jpush.JpushContract;
import com.gurun.codes.databinding.ActivityLoginBinding;
import com.gurun.gateway.MainService;
import com.gurun.model.bean.event_bean.LoginSuccessEvent;
import com.gurun.model.bean.user.LoginResultBean;
import com.jakewharton.rxbinding2.view.RxView;

import org.greenrobot.eventbus.EventBus;

import io.reactivex.functions.Consumer;

public class LoginActivity extends BaseAppCompatActivity implements LoginActivityContract.LoginActivityView, JpushContract.JpushView {
    private ActivityLoginBinding mBinding;
    private LoginActivityContract.LoginActivityViewPresenter mPresenter;
    private ProgressDialog mProgressDialog;
    private JpushContract.JpushPresenter mJpushPresenter;

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, LoginActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        startGatway();
        if (App.getInstance().isLogin(this)) {
            startMainActivity();
            return;
        }
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_login);
        mPresenter = new LoginActivityContract.LoginActivityViewPresenter(this);
        mPresenter.bind(this);
        initView();
        initDatas();
        initEvent();
    }

    private void startGatway() {
        //启动网关service
        mJpushPresenter = new JpushContract.JpushPresenter(this);
        mJpushPresenter.bind(this);
        mJpushPresenter.bindPushChannelIdToServer();
        startService(new Intent(mContext, MainService.class));
        Logger.d("mac address:" + CommonUtils.getAndoridId(mContext));
//        ToastManager.show(this,"mac地址:"+CommonUtils.getAndoridId(mContext), Toast.LENGTH_LONG);
    }

    private void startMainActivity() {
        MainActivityTwo.startActivity(this);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.unbind();
        }
        if (mJpushPresenter != null) {
            mJpushPresenter.unbind();
        }
    }

    protected void initView() {

    }

    @Override
    protected void initDatas() {

    }

    protected void initEvent() {
        RxView.clicks(mBinding.btnLogin)
                .compose(new DebounceObservableTransformer<>())
                .compose(this.bindToLifecycle())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        String account = mBinding.edtAccount.getText().toString().trim();
                        String password = mBinding.edtPassword.getText().toString().trim();
                        if (TextUtils.isEmpty(account)) {
                            ToastManager.show(mContext, R.string.login_error_account);
                            return;
                        }
                        if (TextUtils.isEmpty(password)) {
                            ToastManager.show(mContext, R.string.login_error_password);
                            return;
                        }
                        mPresenter.login(account, password);
                    }
                });
    }


    @Override
    public void onComplete() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }

    @Override
    public void onLoading() {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(mContext);
            mProgressDialog.setMessage(getString(R.string.login_ing));
        }
        mProgressDialog.show();
    }

    @Override
    public void onLoaded(LoginResultBean datas) {
        if (datas != null) {
            ToastManager.show(mContext, R.string.login_success);
            App.getInstance().setLoginToken(
                    datas.getToken());
            App.getInstance().setLoginUserId(
                    datas.getUserId());
            App.getInstance().setUserName(datas.getNickname());
            App.getInstance().setUserPhoneNum(datas.getLoginName());
            EventBus.getDefault().post(new LoginSuccessEvent(datas.getNickname()));
            startMainActivity();
        }
    }

    @Override
    public void onError(String msg) {
        ToastManager.show(mContext, msg);
    }
}
